<template>
  <div>
    <el-form ref="loginForm" :model="form" :rules="rules" hide-required-asterisk>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" show-password />
      </el-form-item>
      <el-form-item>
        <el-button class="loginButton" type="primary" @click="clickLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  name: 'AddRegister',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async clickLogin () {
      try {
        const valid = await this.$refs.loginForm.validate()
        if (valid) {
          const res = await login(this.form)
          console.log(res)
          // 向仓库中存token
          this.$store.commit('user/setUserToken', res.token)
          this.$router.push('/')
        }
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="less" scoped>
*{
  margin: 0;
  padding: 0;
}
.login-page{
  min-height: 100vh;
  background: url(@/assets/images/login_bg.jpg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.el-card {
  width: 420px;
    a{
      text-decoration: none;
      color: blue;
      font-size: 12px;
    }
  // 深度作用选择器   ::v-deep   /deep/
  ::v-deep .el-card__header {
    // background: rgba(114,124,245,1);
    text-align: center;
    color: black;
    font-size: 20px;
  }
  .loginButton{
    width: 380px;
  }
  .loginTitle{
    text-align: center;
    margin: 20px;
      p{
        font-size: 24px;
        font-weight: 600;
      }
  }
}
</style>
